package com.x25175.file.controller;

import com.x25175.file.Vo.ResponseVO;
import com.x25175.file.entity.User;
import com.x25175.file.service.UserService;
import com.x25175.file.util.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author 徐佳辉
 * @date 2020/11/18 15:54
 */
@Controller
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user 前端只获取两个字段用户名和密码
     * @param session
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseVO login(User user, HttpSession session) {
        log.info("User:[{}]", user);
        user.setUserName(user.getUserName().trim());
        user.setPassword(user.getPassword().trim());
        boolean b = UserNull(user);
        if (b) {
            return ResponseVO.error("不能为空");
        }
        User user1 = userService.selectByUserName(user.getUserName());
        if (user1 == null) {
            return ResponseVO.error("用户名或密码错误");
        }
        if (MD5.md5(user.getPassword()).equals(user1.getPassword())) {
            session.setAttribute("user", user1);
            return ResponseVO.ok("登录成功");

        } else {
            return ResponseVO.error("用户名或密码错误");
        }

    }

    /**
     * 注册
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping("/postregister")
    @ResponseBody
    public ResponseVO register(String userName, String password) {
        userName.trim();
        password.trim();
        log.info("username:[{}],password:[{}]", userName, password);
        User user = userService.selectByUserName(userName);
        if (user == null) {
            User userCreate = new User();
            userCreate.setCreateTime(new Date());
            userCreate.setPassword(MD5.md5(password));
            userCreate.setStudentId(-1);
            userCreate.setUserName(userName);
            boolean insert = userService.insert(userCreate);
            return insert ? ResponseVO.ok("注册成功") : ResponseVO.error("注册失败");
        } else {
            return ResponseVO.error("用户已存在，请重新注册");
        }


    }


    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    @ResponseBody
    public ResponseVO logout(HttpSession session) {
        session.removeAttribute("user");
        return ResponseVO.ok("退出成功");
    }


    /**
     * 修改密码
     * @param oldpwd 旧密码
     * @param newpwd 新密码
     * @param session session获取用户名
     * @return
     */
    @PostMapping("/change")
    @ResponseBody
    public ResponseVO change(String oldpwd, String newpwd, HttpSession session) {
        log.info("old:[{}],new:[{}]",oldpwd,newpwd);
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return ResponseVO.error("请登录后修改");
        }
        String userName = user.getUserName();
        User user1 = userService.selectByUserName(userName);
        if (user1.getPassword().equals(MD5.md5(oldpwd))) {
            user1.setPassword(MD5.md5(newpwd));
            userService.updateByPrimaryKey(user1);
            return ResponseVO.ok("修改成功");
        } else {
            return ResponseVO.error("密码错误");
        }


    }

    /**
     * true为空
     *
     * @param user
     * @return
     */
    private boolean UserNull(User user) {
        String userName = user.getUserName();
        String password = user.getPassword();
        boolean ps = StringUtils.isEmpty(password);
        boolean na = StringUtils.isEmpty(userName);
        return ps || na;
    }

}
